package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.ClassRoom;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.ClassRoomQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IClassRoomService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("classRoom")
public class ClassRoomController {

    @Autowired
    private IClassRoomService classRoomService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("classRoom:index")
    @PermissionName("教室管理主页")
    @RequestMapping("")
    public String index(){
        return "classRoom/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequiresPermissions("classRoom:list")
    @PermissionName("教室管理数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ClassRoomQueryObject qo){
        PageResult result = classRoomService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequiresPermissions("classRoom:listAll")
    @PermissionName("教室管理全部数据")
    @RequestMapping("listAll")
    @ResponseBody
    public List<ClassRoom> listAll(){
        return classRoomService.selectAll();
    }

    /**
     * 删除
     * @param classRoomId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("classRoom:delete")
    @PermissionName("教室管理删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long classRoomId){
        JsonResult result = new JsonResult();
        try{
            classRoomService.deleteByPrimaryKey(classRoomId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param classRoom 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("classRoom:update")
    @PermissionName("教室管理编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(ClassRoom classRoom){
        JsonResult result = new JsonResult();
        try{
            classRoomService.updateByPrimaryKey(classRoom);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param classRoom 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("classRoom:save")
    @PermissionName("教室管理保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(ClassRoom classRoom){
        JsonResult result = new JsonResult();
        try{
            classRoomService.insert(classRoom);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


    /**
     * 改变教室的状态
     * @param classRoomId 根据教室的id来进行
     * @return  返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("classRoom:changeState")
    @PermissionName("教室管理状态")
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long classRoomId){
        JsonResult result = new JsonResult();
        try{
            classRoomService.changeState(classRoomId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

}
